<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="doAdd">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button size="small" type="success" @click="doAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="doEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delRoles(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :page-size="pageparams.pagesize"
                :current-page.sync="pageparams.page"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit ? '编辑': '新增'+'部门' "
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="doClose"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="doCancle">取消</el-button>
          <el-button size="small" type="primary" @click="doSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限 -->
    <el-dialog :visible.sync="showDialogAssign" title="分配权限">
      <assignPermission v-if="showDialogAssign" :cur-id="assignCurId" @close="hClose" @success="hSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, getDelRole, submitForm, editRole } from '@/api/settings'
import assignPermission from './assignPermission.vue'
export default {
  components: { assignPermission },
  data() {
    return {
      // 角色列表
      roleList: [],
      // 列表长度
      total: 0,
      pageparams: {
        page: 1,
        pagesize: 2
      },
      // 是否显示弹窗
      showDialog: false,
      // 分配权限弹窗
      showDialogAssign: false,
      // 表单数据
      roleForm: {
        name: '',
        description: ''
      },
      // 校验规则
      rules: {

      },
      // 判断是否是编辑
      isEdit: false,
      // 分配权限当前id
      assignCurId: ''

    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 加载角色数据
    async loadRoles() {
      const { data: res } = await getRoles(this.pageparams)
      console.log('角色数据....', res)
      this.roleList = res.data.rows
      this.total = res.data.total
    },
    hCurrentChange(curpage) {
      console.log(`用户点击了第${curpage}页`)
      // 把当前页码给参数中的page(更新查询中的页码)
      this.pageparams.page = curpage
      // 重发请求
      this.loadRoles()
    },
    // 删除
    delRoles(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log('id是....', id)
        const { data: res } = await getDelRole(id)
        console.log('删除角色', res)

        // 如果删除是最后一条数据，则页码要-1
        if (this.roleList.length === 1) {
          let page = this.pageparams.page
          page--
          // 页码不能减是0
          // if (this.pageParams.page === 0) {
          //   this.pageParams.page = 1
          // }
          page = Math.max(1, page)
          this.pageparams.page = page
        }
        this.$message.success('删除成功')
        // 更新页面
        this.loadRoles()
      }).catch(() => {
      })
    },
    // 弹出弹窗
    doAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 提交表单数据
    async doSubmit() {
      if (this.isEdit) {
        await editRole(this.roleForm)
        // console.log('编辑表单数据',res);
        // 关闭弹窗

        this.showDialog = false
        this.$message.success('更新成功')
        // 刷新页面
        this.loadRoles()
      } else {
        // 新增角色
        await submitForm(this.roleForm)
        // 新增成功之后total+1
        this.total++
        // Math.ceil()向上取整法
        this.pageparams.page = Math.ceil(this.total / this.pageparams.pagesize)
        // console.log('提交信息', res);
        // 关闭弹窗
        this.showDialog = false
        // 刷新页面
        this.loadRoles()
      }
    },
    // 编辑表单
    doEdit(row) {
      this.roleForm = { ...row }
      // 显示弹窗
      this.showDialog = true
      this.isEdit = true
    },
    doCancle() {
      this.showDialog = false
    },
    // 重置表单
    doClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    // 分配权限
    doAssign(id) {
      this.showDialogAssign = true
      this.assignCurId = id
    },
    // 关闭分配权限弹窗
    hClose() {
      this.showDialogAssign = false
    },
    // 更新分配权限成功,关闭弹层
    hSuccess() {
      this.showDialogAssign = false
    }
  }

}
</script>

<style>

</style>
